package awardmanager.panelreport;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

@SuppressWarnings("unused")

public class PanelReportByStartDate_PanID {
	WebDriver driver;

	@BeforeTest
	public void setUp(){
		driver = new FirefoxDriver();	
	}
	@AfterTest
	public void tearDown(){
		driver.quit();
	}
	@Test
	public void proListStartDate() throws InterruptedException{

		//driver.get(awardmanager.Global.AccURL);
		driver.get("https:\\research.gov");

		Select select = new Select (driver.findElement(By.name("eauthurlselector")));
		select.selectByVisibleText("NSF Staff");

		driver.findElement(By.xpath("//*[@value='Login']")).click();

		WebElement waitForLogin = (new WebDriverWait(driver, 10))
				.until(new ExpectedCondition<WebElement>(){
					@Override
					public WebElement apply(WebDriver d) {
						return d.findElement(By.name("IDToken1"));
					}});

		driver.findElement(By.name("IDToken1")).sendKeys(awardmanager.Global.StaffUN);
		driver.findElement(By.name("IDToken2")).sendKeys(awardmanager.Global.StaffPW);
		driver.findElement(By.name("Login")).click();

		WebElement waitForDash = (new WebDriverWait(driver, 10))
				.until(new ExpectedCondition<WebElement>(){
					@Override
					public WebElement apply(WebDriver d) {
						return d.findElement(By.linkText("Award Manager"));
					}});

		driver.get("https://identity.research.gov/sso/SAMLPOSTProfileServlet?TARGET=http://dashboard11g.research.gov/analytics/");

		Alert alert = driver.switchTo().alert();
		alert.accept();

		WebElement waitForSearchDash = (new WebDriverWait(driver, 10))
				.until(new ExpectedCondition<WebElement>(){
					@Override
					public WebElement apply(WebDriver d) {
						return d.findElement(By.linkText("Help"));
					}});

		driver.get(awardmanager.Global.PropPanelStartDate_ID);

		driver.findElement(By.xpath("(//span/input)[1]")).sendKeys("12/01/2011");
		driver.findElement(By.xpath("(//span/input)[2]")).sendKeys("01/01/2012");
		driver.findElement(By.name("gobtn")).click();

		WebElement waitForPanelTable1 = (new WebDriverWait(driver, 10))
				.until(new ExpectedCondition<WebElement>(){
					@Override
					public WebElement apply(WebDriver d) {
						return d.findElement(By.xpath("//table[@class='CVTable']"));
					}});

		driver.findElement(By.xpath("//*[@class='TapeDeckCell']/img[@class='TapeDeckImageEna'][2]")).click();

		Thread.sleep(3000);

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy_MM_dd_hhmmss");  
		Date curDate = new Date();  
		String exeDate = sdf.format(curDate);  
		String fileName = "PanStartDate_PropID" + exeDate + ".csv"; 
		
		// Grab the table
		List<WebElement> allRows = driver.findElements(By.xpath("//td[@class='PTChildPivotTable']"));

		// And iterate over them, getting the cells
		for (WebElement row : allRows) {
			List<WebElement> cells = row.findElements(By.xpath("//td[@class='PTChildPivotTable']//td[3]"));
			for (WebElement cell : cells) {
				Object CellValue = cell.getText();

				System.out.println(CellValue.toString());
				//Write out to a file
				try{
					FileWriter writer = new FileWriter("src/awardmanager/data/" + fileName , true);
					BufferedWriter out = new BufferedWriter(writer);
					out.flush();
					out.write(CellValue.toString() + "\r\n");
					out.close();
				}
				catch (Exception e){//Catch exception if any
					System.err.println("Error: " + e.getMessage());
				}}}
	}

}
